package algorithm.view.sort.insert;

import algorithm.view.common.AlgoHelpler;
import algorithm.view.prepare.Circle;

import javax.swing.*;
import java.awt.*;

/**
 * @author bingo
 * @Description
 * @Date 2018/4/23
 */
public class AlgoFrame extends JFrame {

    private  int  canvasWeight ;

    private int canvasHeight;

    public AlgoFrame(String title, int canvasWeight, int canvasHeight)  {
        super(title);
        this.canvasWeight = canvasWeight;
        this.canvasHeight = canvasHeight;

         AlgoCanvas algoCanvas = new AlgoCanvas();
         setContentPane(algoCanvas);
         pack();
         setResizable(false);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
    }

    public  AlgoFrame(String title){
         this(title,1024,768);
    }

    public int getCanvasWeight() {
        return canvasWeight;
    }

    public void setCanvasWeight(int canvasWeight) {
        this.canvasWeight = canvasWeight;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    public void setCanvasHeight(int canvasHeight) {
        this.canvasHeight = canvasHeight;
    }

    private  InsertSortData insertSortData;

    public void render(InsertSortData insertSortData){
      this.insertSortData = insertSortData;
        repaint();
    }
    private class AlgoCanvas extends  JPanel{
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            Graphics2D graphics2D = (Graphics2D)g;
            int strokeWeight = 1;
             AlgoHelpler.setStrokeWeight(graphics2D,strokeWeight);
             int w = canvasWeight/insertSortData.N();
             for(int i=0;i<insertSortData.N();i++){
                 if(i<insertSortData.orderedIndex){
                     AlgoHelpler.setColor(graphics2D,Color.orange);
                 }
                 else{
                     AlgoHelpler.setColor(graphics2D,Color.lightGray);
                 }
                 if(i==insertSortData.currentIndex){
                     AlgoHelpler.setColor(graphics2D,Color.blue);
                 }
                 if(i==insertSortData.compareToIndex){
                     AlgoHelpler.setColor(graphics2D,Color.red);
                 }
                 AlgoHelpler.fillRectangle(graphics2D,i*w,canvasHeight-insertSortData.get(i),w-1,insertSortData.get(i));
             }

        }
        @Override
        public  Dimension getPreferredSize(){
            return   new Dimension(canvasWeight,canvasHeight);
        }
    }

}
